import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = () => import(/* webpackChunkName: "Login_Home_Wel" */ '../components/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Wel" */ '../views/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Wel" */ '../views/Welcome.vue')
const Users = () => import(/* webpackChunkName: "Params" */ '../views/user/Users.vue')
const Rights = () => import(/* webpackChunkName: "Params" */ '../views/premission/Rights.vue')
const Roles = () => import(/* webpackChunkName: "Params" */ '../views/premission/Roles.vue')
const Cate = () => import(/* webpackChunkName: "Goods" */ '../views/goods/Cate.vue')
const Params = () => import(/* webpackChunkName: "Goods" */ '../views/goods/Params.vue')
const List = () => import(/* webpackChunkName: "List" */ '../views/goods/List.vue')
const GoodsAdd = () => import(/* webpackChunkName: "List" */ '../views/goods/Add.vue')
const Order = () => import(/* webpackChunkName: "List" */ '../views/orders/Order.vue')
const Report = () => import(/* webpackChunkName: "echars" */ '../views/report/Report.vue')
// import Home from '../views/Home.vue'
// import Welcome from '../views/Welcome.vue'
// import Users from '../views/user/Users.vue'
// import Rights from '../views/premission/Rights.vue'
// import Roles from '../views/premission/Roles.vue'
// import Cate from '../views/goods/Cate.vue'
// import Params from '../views/goods/Params.vue'
// import List from '../views/goods/List.vue'
// import GoodsAdd from '../views/goods/Add.vue'
// import Order from '../views/orders/Order.vue'
// import Report from '../views/report/Report.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login,
    name: 'login'
  },
  {
    path: '/home',
    component: Home,
    name: 'home',
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: Welcome,
        component: Welcome
      },
      {
        path: '/users',
        name: Users,
        component: Users
      },
      {
        path: '/rights',
        name: Rights,
        component: Rights
      },
      {
        path: '/roles',
        name: Roles,
        component: Roles
      },
      {
        path: '/categories',
        name: Cate,
        component: Cate
      },
      {
        path: '/params',
        name: Params,
        component: Params
      },
      {
        path: '/goods',
        name: List,
        component: List
      },
      {
        path: '/goods/add',
        name: GoodsAdd,
        component: GoodsAdd
      },
      {
        path: '/orders',
        name: Order,
        component: Order
      },
      {
        path: '/reports',
        name: Report,
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 去登录页放行
  if (to.name === 'login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token 去登录页
  if (!tokenStr) return next('/login')
  // 有token 放行
  next()
})

export default router
